package com.gxar.marketdelivery.domain.aviator.parser;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.gxar.marketdelivery.application.context.DeliveryContext;
import com.gxar.marketdelivery.shared.model.DeliveryMaterialModel;
import com.gxar.marketdelivery.shared.utils.CollectionUtils;
import com.gxar.marketdelivery.shared.utils.JsonSchemaUtil;
import com.gxar.marketdelivery.shared.utils.StringUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author nice
 * @version 1.0
 * @created 2023/8/3.
 */
public class MaterialParser {

    public static Map<Long, String> parse(DeliveryContext context, List<DeliveryMaterialModel> materialModels) {
        if (CollectionUtils.isEmpty(materialModels)) {
            return Collections.emptyMap();
        }
        Map<Long, String> result = new HashMap<>();
        materialModels.forEach((materialModel) -> {
            ObjectNode materialNode = normalizeStaticObjectNode(materialModel);
            result.put(materialModel.getTargetId(), materialNode.toString());
        });
        return result;
    }

    /**
     * 解析静态对象
     *
     * @param materialModel
     * @return
     */
    private static ObjectNode normalizeStaticObjectNode(DeliveryMaterialModel materialModel) {
        String staticSchemaValue = materialModel.getStaticSchemaValue();
        ObjectNode staticNode;
        if (StringUtils.isBlank(staticSchemaValue)) {
            staticNode = JsonSchemaUtil.createObjectNode();
        } else {
            staticNode = JsonSchemaUtil.parseJson(staticSchemaValue);
        }
        return staticNode;
    }
}
